package two.chapter_1.c1_4;

import edu.princeton.cs.algs4.StdOut;
import edu.princeton.cs.algs4.StdRandom;

public class HotOrCold {

    private int mystery;
    private int N;
    private int [] arr;
    public HotOrCold(int N){
        mystery=4;//StdRandom.uniform(1,N);
        StdOut.println("mystery:"+mystery);
        arr=new int[N];
        for (int i=1;i<=N;i++) {
            arr[i-1]=i;
        }
    }
    public int get(int index){
        return arr[index];
    }
    public int findMystery(){

        int lo=0;
        int hi=arr.length-1;
        while (lo<hi){
            int mid=lo+(hi-lo)/2;
            int code=guess(lo);
            if (code==CODE_EQUAL){
                return lo;
            }
            code=guess(hi);
            if (code==CODE_EQUAL){
                return hi;
            }else if (code==CODE_HOT){
                lo=mid;
            }else {
                hi=mid;
            }




        }

        return -1;
    }

    private int getDelta(int index){
        return arr[index]-mystery;
    }
    private int prevDelta=Integer.MAX_VALUE;
    private int guess(int guess){
        if (arr[guess]==mystery){
            return CODE_EQUAL;
        }
        int delta=Math.abs(arr[guess]-mystery);
        int code=delta-prevDelta;
        if (code>0){
            return CODE_COLD;
        }else {
            return CODE_HOT;
        }

    }

    public static final int CODE_EQUAL=0;
    public static final int CODE_HOT=-1;
    public static final int CODE_COLD=-2;
}
